package com.zscat.mallplus.utils;

import com.dianping.dzopen.constants.AppConstants;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MeituanUtil {
    public String getMeituanToken(String appKey,String appSecret,String authCode,String redirectUrl) throws IOException {
        Map<String, String> requestParam = new HashMap<>();
        requestParam.put("app_key", appKey);
        requestParam.put("app_secret", appSecret);
        requestParam.put("auth_code", authCode);
        requestParam.put("grant_type", "authorization_code");
        requestParam.put("redirect_url", redirectUrl);
// 构造form
        Form form = Form.form();
        for(Map.Entry<String, String> entry : requestParam.entrySet()){
            form.add(entry.getKey(), entry.getValue());
        }
// 发起post请求
        return Request.Post(AppConstants.HOST + "/router/oauth/token").bodyForm(form.build())
                .setHeader("Content-Type", ContentType.create("application/x-www-form-urlencoded","UTF-8")
                        .toString()).execute().returnContent().asString();
    }


    public String refreshToken(String appKey,String appSecret,String refreshSession) throws IOException {
        Map<String, String> requestParam = new HashMap<>();
        requestParam.put("app_key", appKey);
        requestParam.put("app_secret", appSecret);
        requestParam.put("refresh_token", refreshSession);
        requestParam.put("grant_type", "refresh_token");
// 构造form
        Form form = Form.form();
        for(Map.Entry<String, String> entry : requestParam.entrySet()){
            form.add(entry.getKey(), entry.getValue());
        }
// 发起post请求
        return Request.Post(AppConstants.HOST + "/router/oauth/token").bodyForm(form.build())
                .setHeader("Content-Type", ContentType.create("application/x-www-form-urlencoded","UTF-8")
                        .toString()).execute().returnContent().asString();
    }
}
